//index.js
const app = getApp()

const dtFmt = require('../../lib/utils.js')

Page({
  data: {
    bills: [],
    height: '',
    pageNum: 1,
    pageSize: 8,
    hasMore: true,
    toggleItem: '',
  },

  onLoad: function () {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
    this.queryData()
    wx.stopPullDownRefresh()
  },

  //下拉刷新
  onPullDownRefresh: function () {
    console.log('invoiceHistory onPullDownRefresh')
    this.setData({
      pageNum: 1,
      bills: [],
      hasMore: true
    })
    this.onLoad()
  },

  //到达底部加载
  onReachBottom: function() {
    if (this.data.hasMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      })
      this.queryData()
    }
  },

  onShow: function () {
    
  },

  onUnload: function() {
    wx.removeStorage({
      key: 'bills',
      success: function(res) {
        console.log("remove storage => ", res)
      },
    })
    this.setData({
      bills: []
    })
  },

  queryData: function() {
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    wx.cloud.callFunction({
      name: 'bill_query',
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      success: res => {
        wx.hideLoading()
        console.log("invoiceHistory bill_query result => ", res)
        for (var index in res.result.data) {
          //console.log(new Date(res.result.data[index].createDate).toLocaleString())
          res.result.data[index].createDate = dtFmt.tsFormatTime(res.result.data[index].createDate, 'Y-M-D h:m:s')
        }
        
        if (res.result.data.length > 0) {
          var newBills = this.data.bills.concat(res.result.data)
          console.log("invoiceHistory bill_query bills => ", newBills)
          this.setData({
            bills: newBills
          })
          wx.setStorage({
            key: 'bills',
            data: this.data.bills,
            complete: res => {
              console.log("set storage => ", res)
            }
          })
          if (!this.data.hasMore) {
            this.setData({
              hasMore: true
            })
          }
        } else {
          if (this.data.hasMore) {
            this.setData({
              hasMore: false
            })
            wx.showToast({
              title: '没有更多啦~',
            })
          }
        }
        
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '账单发票信息加载失败',
        })
        console.error('[云函数] [bill_query] 调用失败：', err)
      }
    })
  },

  deleteBill: function(e) {
    // wx.showLoading({
    //   title: '删除中',
    // })
    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 2000)
    var _this = this
    wx.cloud.callFunction({
      name: 'bill_delete_by_id',
      data: {
        billId: e.currentTarget.dataset.bid,
      },
      success: res => {
        // wx.hideLoading()
        console.log("invoiceHistory bill_delete_by_id result => ", res)
        _this.data.bills.splice(e.currentTarget.dataset.idx, 1)
        _this.setData({
          bills: this.data.bills
        })
        // wx.showToast({
        //   icon: 'none',
        //   title: '删除成功',
        // })
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '删除失败',
        })
        console.error('[云函数] [bill_delete_by_id] 调用失败：', err)
      }
    })
  },

  //点击切换隐藏和显示
  toggleBtn: function (event) {
    //console.log("toggle => ", event)
    // console.log("toggle => ", this.data)
    if (this.data.toggleItem == event.currentTarget.dataset.bid) {
      this.setData({
        toggleItem: ''
      })
    } else {
      this.setData({
        toggleItem: event.currentTarget.dataset.bid
      })
    }
  },
})